package com.wsx.sky.gateway.outter.http;

import com.google.gson.Gson;
import com.wsx.sky.gateway.bean.GateData;
import com.wsx.sky.gateway.bean.GateParam;
import com.wsx.sky.gateway.outter.BaseController;
import com.wsx.sky.gateway.outter.http.service.HttpFeignService;
import com.wsx.sky.http.HttpClientUtils;
import com.wsx.sky.http.bean.HttpRequest;
import com.wsx.sky.http.bean.HttpResponse;
import org.apache.skywalking.apm.toolkit.trace.TraceContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @author wangshuaixin
 * @Title: com.wsx.sky.gateway.outter.http.HttpController
 * @Description: 基于http协议的请求调用，主要涉及到外部http服务的调用
 * @date 2018/12/10
 */
@RestController
public class HttpController extends BaseController {


    private static final String GET_HTTP = "http://localhost:8131/http/client/get/feign/";
    private static final String POST_HTTP = "http://localhost:8131/http/client/request/feign";

    private Gson gson = new Gson();

    @Autowired
    private HttpFeignService httpFeignService;

    @RequestMapping(
            value = "/http/get/{name}",
            method = {RequestMethod.GET}
    )
    public GateData hget(@PathVariable("name") String name) {
        String traceid = TraceContext.traceId();
        System.out.println(traceid);
        if (StringUtils.isEmpty(name)) {
            return new GateData(30001).buildDatas("gate param is error");
        }
        try {
            String val = HttpClientUtils.get(GET_HTTP + name, null);
            if (!StringUtils.isEmpty(val)) {
                return new GateData().buildDatas(val);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new GateData(50001);
    }

    @RequestMapping(
            value = "/http/http/get/{name}",
            method = {RequestMethod.GET}
    )
    public GateData hhget(@PathVariable("name") String name) {
        String traceid = TraceContext.traceId();
        System.out.println(traceid);
        if (StringUtils.isEmpty(name)) {
            return new GateData(30001).buildDatas("gate param is error");
        }
        try {
            HttpRequest httpRequest = new HttpRequest();
            httpRequest.setName(name);
            httpRequest.setHid(112L);
            httpRequest.setTraceid(traceid);
            String val = HttpClientUtils.execPost("http://localhost:8131/http/client/request/http", gson.toJson(httpRequest), null);
            if (!StringUtils.isEmpty(val)) {
                return new GateData().buildDatas(val);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new GateData(50001);
    }

    @RequestMapping(
            value = "/http/post",
            method = {RequestMethod.POST}
    )
    public GateData hpost(@RequestBody GateParam gateParam) {
        String traceid = TraceContext.traceId();
        System.out.println(traceid);
        if (null == gateParam ||
                StringUtils.isEmpty(gateParam.getName())) {
            return new GateData(30001).buildDatas("gate param is error");
        }
        try {
            HttpRequest request = new HttpRequest();
            request.setName(gateParam.getName());
            request.setHid(new Long(gateParam.getName().hashCode()));
            request.setTraceid(traceid);
            String val = HttpClientUtils.execPost(POST_HTTP,  gson.toJson(request), null);
            if (!StringUtils.isEmpty(val)) {
                HttpResponse response = gson.fromJson(val, HttpResponse.class);
                return new GateData().buildDatas(response.getMsg());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new GateData(50001);
    }


    @RequestMapping(
            value = "/feign/get/{name}",
            method = {RequestMethod.GET}
    )
    public GateData fget(@PathVariable("name") String name) {
        if (StringUtils.isEmpty(name)) {
            return new GateData(30001).buildDatas("gate param is error");
        }
        try {
            HttpResponse response = httpFeignService.get(name);
            return new GateData(response.getCode()).buildDatas(response.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new GateData(50001);
    }

    @RequestMapping(
            value = "/feign/post",
            method = {RequestMethod.POST}
    )
    public GateData fpost(@RequestBody GateParam gateParam) {
        String traceid = TraceContext.traceId();
        System.out.println(traceid);
        if (null == gateParam ||
                StringUtils.isEmpty(gateParam.getName())) {
            return new GateData(30001).buildDatas("gate param is error");
        }
        try {
            HttpRequest request = new HttpRequest();
            request.setName(gateParam.getName());
            request.setHid(1123L);
            request.setTraceid(traceid);
            HttpResponse response = httpFeignService.post(request);
            return new GateData(response.getCode()).buildDatas(response.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new GateData(50001);
    }

    @RequestMapping(
            value = "/feign/one/get/{name}",
            method = {RequestMethod.GET}
    )
    public GateData foget(@PathVariable("name") String name) {
        if (StringUtils.isEmpty(name)) {
            return new GateData(30001).buildDatas("gate param is error");
        }
        try {
            HttpResponse response = httpFeignService.redis(name);
            return new GateData(response.getCode()).buildDatas(response.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new GateData(50001);
    }

}
